package org.linuxsogood.boot.config;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @author honway.liu
 * @date 2018/8/5 下午10:34
 * @email gm100861@gmail.com
 * @blog http://linuxsogood.org
 */
@ControllerAdvice
public class ExceptionAdvisor {

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    @ResponseStatus
    public ApiResponse exceptionHandler() {
        return new ApiResponse(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase());
    }

    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    public ApiResponse formatCheckExceptionHandler(RuntimeException e) {
        return new ApiResponse(HttpStatus.BAD_REQUEST.value(), e.getMessage());
    }
}
